#include "picstatebutton.h"
#include <QPushButton>
#include "const.h"
#include <QEvent>
PicStateButton::PicStateButton(QWidget *parent)
    :QPushButton(parent)
{

}

void PicStateButton::SetIcons(const QString &normalShow, const QString &hoverShow, const QString &pressedShow,
                              const QString &normalSuspend, const QString &hoverSuspend, const QString &pressedSuspend)
{
    _normalShow = normalShow;
    _hoverShow = hoverShow;
    _pressedShow = pressedShow;

    _normalSuspend = normalSuspend;
    _hoverSuspend = hoverSuspend;
    _pressedSuspend = pressedSuspend;

    QPixmap tmpPixmap;
    tmpPixmap.load(normalShow);
    this->resize(tmpPixmap.size());
    this->setIcon(tmpPixmap);
    this->setIconSize(tmpPixmap.size());
    _cur_state = PicBtnStateNormal;
}

bool PicStateButton::event(QEvent *e)
{
    auto type = e->type();
    if(type == QEvent::Enter){
        if(_cur_state < PicBtnSuspendStateNormal){
            setHoverIcon();
        }else{
            setHoverSuspendIcon();
        }
    }else if(type == QEvent::Leave){
        if(_cur_state < PicBtnSuspendStateNormal){
            setNormalIcon();
        }else{
            setNormalSuspendIcon();
        }
    }else if(type == QEvent::MouseButtonPress){
        if(_cur_state < PicBtnSuspendStateNormal){
            setPressIcon();
        }else{
            setPressSuspendIcon();
        }
    }else if(type == QEvent::MouseButtonRelease){
        if(_cur_state < PicBtnSuspendStateNormal){
            setHoverSuspendIcon();
        }else{
            setHoverIcon();
        }
    }
    return QPushButton::event(e);
}

void PicStateButton::SlotStart()
{
    setNormalIcon();
}

void PicStateButton::SlotStop()
{
    setNormalSuspendIcon();
}

void PicStateButton::setNormalIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_normalShow);
    this->setIcon(tmpPixmap);
    _cur_state = PicBtnStateNormal;
}

void PicStateButton::setHoverIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_hoverShow);
    this->setIcon(tmpPixmap);
    _cur_state = PicBtnStateHover;
}

void PicStateButton::setPressIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_pressedShow);
    this->setIcon(tmpPixmap);
    _cur_state = PicBtnStatePress;
}

void PicStateButton::setNormalSuspendIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_normalSuspend);
    this->setIcon(tmpPixmap);
    _cur_state = PicBtnSuspendStateNormal;
}

void PicStateButton::setHoverSuspendIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_hoverSuspend);
    this->setIcon(tmpPixmap);
    _cur_state = PicBtnSuspendHover;
}

void PicStateButton::setPressSuspendIcon()
{
    QPixmap tmpPixmap;
    tmpPixmap.load(_pressedSuspend);
    this->setIcon(tmpPixmap);
    _cur_state = PicBtnSuspendPress;
}

